package com.naodong.shenluntiku.mvp.view.activity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerNoticeSetComponent;
import com.naodong.shenluntiku.di.module.NoticeSetModule;
import com.naodong.shenluntiku.mvp.contract.NoticeSetContract;
import com.naodong.shenluntiku.mvp.model.bean.NoticeSet;
import com.naodong.shenluntiku.mvp.presenter.NoticeSetPresenter;
import com.naodong.shenluntiku.util.HeightAnimUtil;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.util.ToastUtil;

/**
 * Created by hujie on 17/6/9.
 */

public class NoticeSetActivity extends BaseMVPActivity<NoticeSetPresenter> implements NoticeSetContract.View {


    NoticeSet noticeSet;
    @BindView(R.id.statusSC)
    SwitchCompat switchCompat;

    @BindView(R.id.timeTV)
    TextView timeTV;

    @BindView(R.id.timeView)
    View timeView;

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        requestNoticeSet();
    }


    private void requestNoticeSet() {
        mPresenter.requestNoticeSet();
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerNoticeSetComponent.builder().appComponent(appComponent).noticeSetModule(new NoticeSetModule(this)).build().inject(this);
    }


    @OnClick(R.id.timeView)
    public void onTimeViewClick() {

        Calendar calendar = Calendar.getInstance();
        TimePickerDialog dialog = new TimePickerDialog(mActivity, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                String time = (hourOfDay < 10 ? ("0" + hourOfDay) : hourOfDay) + ":" + ((10 > minute) ? ("0" + minute) : minute);
                mPresenter.requestSetNoticeTime(time);
            }
        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
        dialog.show();

    }

    @Override
    protected int layoutId() {
        return R.layout.a_notice_set;
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onRequesrStart() {
        progressHudUtil.showProgressHud();
    }

    @Override
    public void onRequestFinish() {
        progressHudUtil.dismissProgressHud();
    }

    @Override
    public void onNoticeSetSuccess(NoticeSet noticeSet) {
        setData(noticeSet);
    }

    private void setData(NoticeSet noticeSet) {
        this.noticeSet = noticeSet;
        switchCompat.setOnCheckedChangeListener(null);
        switchCompat.setChecked(noticeSet.getStatus() == 1);
        timeTV.setText(noticeSet.getTime());
        switchCompat.setOnCheckedChangeListener((buttonView, isChecked) -> mPresenter.requestSetNoticeSwitch(isChecked));
    }

    @Override
    public void onSetNoticeTimeSuccess(String time) {
        if (noticeSet != null) {
            noticeSet.setTime(time);
            setData(noticeSet);
        } else {
            timeTV.setText(time);
        }
    }

    @Override
    public void onSetNoticeSwitchSuccess(boolean status) {
        if (noticeSet != null) {
            noticeSet.setStatus(status ? 1 : 0);
            setData(noticeSet);
        }
        HeightAnimUtil.animHeightToView(mActivity, timeView, status, 500);

    }

    @Override
    public void onSetNoticeSwitchFail() {
        ToastUtil.makeToast("设置失败,请重试");
        if (noticeSet != null) {
            setData(noticeSet);
        }
    }

    @Override
    public void onSetNoticeTimeFail() {
        ToastUtil.makeToast("设置失败,请重试");
        if (noticeSet != null) {
            setData(noticeSet);
        }
    }
}
